package com.app.system.permission.service;

import com.app.system.permission.dao.DataDictDao;
import com.app.system.permission.en.Enabled;
import com.app.system.permission.model.DataDict;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典
 * Created by wcf-pc on 2018/5/19.
 *
 * @author wcf-pc
 */
@Service
public class DataDictService {

    @Autowired
    private DataDictDao dataDictDao;

    /**
     * 查询一级科目
     *
     * @return
     */
    public List<DataDict> parentList() {
        return this.dataDictDao.parentList();
    }

    public List<DataDict> childrenList(Integer parentId) {
        return this.dataDictDao.childrenList(parentId);
    }

    public DataDict findOne(Integer id) {
        return this.dataDictDao.findOne(id);
    }

    public void save(DataDict dataDict) {
        this.dataDictDao.save(dataDict);
    }

    /**
     * 子项同时删除
     *
     * @param ids
     */
    @Transactional
    public String deleteDataDict(String[] ids) {
        if (ids.length > 0) {
            List<Integer> idsList = Lists.newArrayList();
            for (String id : ids) {
                idsList.add(Integer.valueOf(id));
            }
            this.dataDictDao.deleteByParentIds(idsList);
            this.dataDictDao.deleteByIds(idsList);
        }
        return "ok";
    }

    public DataDict findOneParentId(int parentId) {
        return this.dataDictDao.findOneByParentId(parentId);
    }

    public List<DataDict> findListByParentName(String parentName) {
        return this.dataDictDao.findListByParentName(parentName, Enabled.YES.getIndex());
    }

    public Map<String, DataDict> getMapByParentNameForName(String parentName) {
        Map<String, DataDict> map = Maps.newLinkedHashMap();
        List<DataDict> dataDictList = this.dataDictDao.findListByParentName(parentName, Enabled.YES.getIndex());
        if (dataDictList != null) {
            for (DataDict dataDict : dataDictList) {
                map.put(dataDict.getName(), dataDict);
            }
        }
        return map;
    }

    public Map<String, DataDict> getMapByParentNameForValue(String parentName) {
        Map<String, DataDict> map = Maps.newLinkedHashMap();
        List<DataDict> dataDictList = this.dataDictDao.findListByParentName(parentName, Enabled.YES.getIndex());
        if (dataDictList != null) {
            for (DataDict dataDict : dataDictList) {
                map.put(dataDict.getValue(), dataDict);
            }
        }
        return map;
    }

    public Map<String, String> getMapByParentNameForValue_Name(String attributeName) {
        LinkedHashMap map = new LinkedHashMap();
        List list = this.findListByParentName(attributeName);
        Iterator i$ = list.iterator();

        while(i$.hasNext()) {
            DataDict dd = (DataDict)i$.next();
            map.put(dd.getValue(), dd.getName());
        }

        return map;
    }

    public Map<String, String> getMapByParentNameForName_Value(String attributeName) {
        LinkedHashMap map = new LinkedHashMap();
        List list = this.findListByParentName(attributeName);
        Iterator i$ = list.iterator();

        while(i$.hasNext()) {
            DataDict dd = (DataDict)i$.next();
            map.put(dd.getName(), dd.getValue());
        }

        return map;
    }

    public Map<String, String> getMapByParentNameForValue_Color(String attributeName) {
        LinkedHashMap map = new LinkedHashMap();
        List list = this.findListByParentName(attributeName);
        Iterator i$ = list.iterator();

        while(i$.hasNext()) {
            DataDict dd = (DataDict)i$.next();
            map.put(dd.getValue(), dd.getColor());
        }

        return map;
    }

    public Map<String, String> getMapByParentNameForName_Color(String attributeName) {
        LinkedHashMap map = new LinkedHashMap();
        List list = this.findListByParentName(attributeName);
        Iterator i$ = list.iterator();

        while(i$.hasNext()) {
            DataDict dd = (DataDict)i$.next();
            map.put(dd.getName(), dd.getColor());
        }

        return map;
    }

    public Map<String, Map> getMapByParentNameForMap_Value(String attributeName) {
        LinkedHashMap<String,Map> resultMap = new LinkedHashMap();
        List list = this.findListByParentName(attributeName);
        Iterator i$ = list.iterator();
        Map itemMap = Maps.newHashMap();
        while(i$.hasNext()) {
            itemMap = Maps.newHashMap();
            DataDict dd = (DataDict)i$.next();
            itemMap.put("name",dd.getName());
            itemMap.put("value",dd.getValue());
            itemMap.put("color",dd.getColor());
            itemMap.put("memo",dd.getMemo());
            resultMap.put(dd.getValue(), itemMap);
        }
        return resultMap;
    }
}
